package net.pinnoocle.playservice.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.pinnoocle.playdao.dao.ShopDao;
import net.pinnoocle.playdao.dao.ShopFinanceDao;
import net.pinnoocle.playdao.entity.ShopFinance;
import net.pinnoocle.playdao.entity.page.PageModel;
import net.pinnoocle.playdao.entity.page.PageQuery;
import net.pinnoocle.playdao.entity.shopPc.finance.ShopFinanceDetailVO;
import net.pinnoocle.playdao.entity.shopPc.finance.ShopFinanceListPageVo;
import net.pinnoocle.playdao.entity.shopPc.finance.ShopFinanceListSearchParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 门店收入明细(ShopFinance)表服务实现类
 *
 * @author ye
 * @since 2021-11-13 09:40:36
 */
@Service("shopFinanceService")
public class ShopFinanceServiceImpl extends ServiceImpl<ShopFinanceDao, ShopFinance> {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    ShopDao shopDao;

    /**
     * 门店资产概况
     * @param params
     * @return
     */
    public ShopFinanceDetailVO shopFinanceDetail(ShopFinanceListSearchParams params){
        ShopFinanceDetailVO shopFinanceDetailVO = new ShopFinanceDetailVO();
        if(params.getShopId() == null){
            String shopId = userService.getGlobalShopId();
            params.setShopId(shopId);
        }
        BigDecimal income = shopDao.totalIncomeExpendAmount(params.getShopId(),0,params.getStartTime(),
                params.getEndTime());
        BigDecimal expend = shopDao.totalIncomeExpendAmount(params.getShopId(),1,params.getStartTime(),
                params.getEndTime());
        shopFinanceDetailVO.setIncomeAmount(income.doubleValue());
        shopFinanceDetailVO.setExpendAmount(expend.doubleValue());
        PageModel<ShopFinanceListPageVo> shopFinanceListPageVoPageModel = shopFinanceListPage(params);
        shopFinanceDetailVO.setListPageVoPageModel(shopFinanceListPageVoPageModel);
        return shopFinanceDetailVO;
    }
    /**
     * 门店资产分页
     * @param params
     * @return
     */
    public PageModel<ShopFinanceListPageVo> shopFinanceListPage(ShopFinanceListSearchParams params){
        Map<String,Object> pageMap = new HashMap<>();
        pageMap.put("page",String.valueOf(params.getPage()));
        pageMap.put("pageSize",String.valueOf(params.getPageSize()));


        Page<ShopFinanceListPageVo> list = shopDao.shopFinanceList(
                new PageQuery<ShopFinance>(pageMap).getPage(),params);
        List<ShopFinanceListPageVo> records = list.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new PageModel<>(list, new ArrayList<>());
        }
        return new PageModel<>(list, records);
    }


}